<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者编号" prop="patientSn">
        <el-input
          v-model="queryParams.patientSn"
          placeholder="请输入患者编号"
          clearable
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item label="就诊编号" prop="inHospSn">
        <el-input
          v-model="queryParams.inHospSn"
          placeholder="请输入就诊编号"
          clearable
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item label="状态" prop="fileFlag">
        <el-select v-model="queryParams.fileFlag" placeholder="请选择状态" clearable>
          <el-option label="成功" value="1" />
          <el-option label="等待" value="0" />
          <el-option label="失败" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="queryParams.createTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-row :gutter="10" class="status-row">
        <el-col :span="12">
          <el-radio-group v-model="queryParams.fileFlag" size="medium" @change="handleStatusChange" class="status-group">
            <el-radio-button label="">全部</el-radio-button>
            <el-radio-button label="1">成功</el-radio-button>
            <el-radio-button label="0">等待</el-radio-button>
            <el-radio-button label="2">失败</el-radio-button>
          </el-radio-group>
        </el-col>
        <el-col :span="12" class="right-actions">
        <el-button
            type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
            class="add-button"
          >添加病历</el-button>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-col>
    </el-row>


    <!-- 数据表格卡片 -->
    <el-card class="table-card" shadow="hover">
    <el-table
      v-loading="loading"
      :data="recordList"
        border
        stripe
        :header-cell-style="{ background: '#f5f7fa', color: '#606266', fontWeight: 'bold' }"
        :cell-style="{ padding: '12px 0' }"
        class="record-table"
      >
        <el-table-column label="病历ID" align="center" prop="mrCode" min-width="120" />
        <el-table-column label="患者编号" align="center" prop="patientSn" min-width="120" />
        <el-table-column label="就诊编号" align="center" prop="inHospSn" min-width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.inHospSn || '暂无数据' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="住院病历记录编号" align="center" prop="inMrSn" min-width="150" />
        <el-table-column label="创建时间" align="center" prop="createdT" min-width="180">
          <template slot-scope="scope">
            <span class="time-text">{{ parseTime(scope.row.createdT, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="80">
        <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.fileFlag === '1'" effect="dark" size="mini" class="status-tag">成功</el-tag>
            <el-tag type="warning" v-else-if="scope.row.fileFlag === '0'" effect="dark" size="mini" class="status-tag">等待</el-tag>
            <el-tag type="danger" v-else-if="scope.row.fileFlag === '2'" effect="dark" size="mini" class="status-tag">失败</el-tag>
            <el-tag type="info" v-else effect="dark" size="mini" class="status-tag">未知</el-tag>
        </template>
      </el-table-column>
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-tooltip content="查看" placement="top">
          <el-button
            size="mini"
                type="primary"
            icon="el-icon-view"
                circle
                plain
            @click="handleView(scope.row)"
                class="action-button"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="编辑" placement="top" v-if="scope.row.fileFlag !== '1'">
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-edit"
                circle
                plain
                @click="handleEdit(scope.row)"
                class="action-button"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top" v-if="scope.row.fileFlag !== '1'">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                circle
                plain
                @click="handleDelete(scope.row)"
                class="action-button"
              ></el-button>
            </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
        class="pagination"
    />
    </el-card>
  </div>
</template>

<script>
import { listInpatientRecord, getInpatientRecord, deleteInpatientRecord } from "@/api/ai/inpatientRecord";
import Pagination from "@/components/Pagination";
import { parseTime } from "@/utils";

export default {
  name: "InpatientRecord",
  components: {
    Pagination
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 住院电子病历记录表格数据
      recordList: [],
      // 日期范围
      createTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientSn: null,
        inHospSn: null,
        fileFlag: "",
        createTime:null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表数据（适配后端分页结构）
    getList() {
      this.loading = true;
      listInpatientRecord(this.queryParams).then(response => {
        console.log('接口返回:', response);
        this.loading = false;
        if (response && response.code === 0) {
          this.recordList = response.data.records || [];
          this.total = response.data.total || 0;
          console.log('搜索结果数量:', this.recordList.length);
        } else {
          this.recordList = [];
          this.total = 0;
          this.$message.error("获取列表失败：" + (response.message || '未知错误'));
        }
      }).catch(error => {
        console.error('接口调用异常:', error);
        this.loading = false;
        this.recordList = [];
        this.total = 0;
        this.$message.error("获取列表失败：" + error.message);
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      console.log('点击重置按钮')
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        patientSn: null,
        inHospSn: null,
        fileFlag: "",
        createTime: null
      };
      this.getList()
    },
    /** 状态筛选操作 */
    handleStatusChange() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 日期范围处理 */
    handleDateRangeChange(val) {
      if (val) {
        this.queryParams.startDate = val[0];
        this.queryParams.endDate = val[1];
      } else {
        this.queryParams.startDate = null;
        this.queryParams.endDate = null;
      }
    },
    /** 查看按钮操作 */
    handleView(row) {
      console.log('点击查看详情，行数据:', row);
      console.log('rowkey值:', row.rowkey);

      const rowkey = row.rowkey;
      if (!rowkey) {
        this.$message.error('缺少必要的参数');
        return;
      }

      console.log('准备跳转到详情页，rowkey:', rowkey);
      this.$router.push({
        path: '/ai/inpatientRecord/detail',
        query: { rowkey: rowkey }
      });
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      const rowkey = row.rowkey.toString();
      this.$router.push({ path: '/ai/inpatientRecord/edit', query: { rowkey: rowkey }});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/ai/inpatientRecord/add' });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rowkey = row.rowkey;
      this.$confirm('是否确认删除该住院电子病历记录?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteInpatientRecord(rowkey);
        }).then(() => {
          this.$message.success("删除成功");
          this.getList();
        }).catch(error => {
          console.error(error);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  .filter-card {
    margin-bottom: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);

    .el-form {
      margin-bottom: 15px;
    }

    .el-input {
      .el-input__inner {
        border-radius: 4px;
      }
    }

    .search-button {
      padding: 9px 20px;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
      }
    }
  }

  .status-row {
    display: flex;
    align-items: center;
    margin-top: 10px;

    .status-group {
      .el-radio-button__inner {
        padding: 10px 20px;
        transition: all 0.3s;
      }

      .el-radio-button__orig-radio:checked + .el-radio-button__inner {
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
      }
    }

    .right-actions {
      display: flex;
      justify-content: flex-end;
      align-items: center;

      .add-button {
        margin-right: 15px;
        border-radius: 4px;
        padding: 10px 20px;
        transition: all 0.3s;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        }
      }
    }
  }

  .table-card {
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);

    .record-table {
      margin: 15px 0;
      border-radius: 6px;
      overflow: hidden;

      .time-text {
        font-family: 'Arial', sans-serif;
        color: #606266;
      }

      .status-tag {
        border-radius: 20px;
        padding: 2px 12px;
        font-weight: bold;
      }

      .action-button {
        transition: all 0.3s;
        margin: 0 5px;

        &:hover {
          transform: scale(1.1);
          box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
        }
      }
    }

    .pagination {
      margin-top: 20px;
      text-align: center;
    }
  }

  .date-picker {
    width: 260px;
  }
}

/* 添加一些基本的CSS动画效果 */
.el-table {
  transition: all 0.3s;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  }
}

.el-card {
  transition: all 0.3s;

  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
  }
}
</style>










